// 这里处理异步请求
import {
  put, takeLatest, all, call,
} from 'redux-saga/effects';
import { fetchJokeApi } from '@/pages/joke/api';

import { RECEIVED_JOKE, FETCH_JOKE_FAILED, FETCH_JOKE } from '@/pages/joke/constants';

function* fetchJoke() {
  try {
    const data = yield call(fetchJokeApi);
    yield put({ type: RECEIVED_JOKE, data: data?.Result?.content || '' });
  } catch (e) {
    yield put({ type: FETCH_JOKE_FAILED, data: e.message });
  }
}

function* actionWatcher() {
  yield takeLatest(FETCH_JOKE, fetchJoke);
}

export default function* rootSaga() {
  yield all([
    actionWatcher(),
  ]);
}
